<?php   // for user login
include "include/dbConfig.php";
?>

<?php
$name = $_REQUEST['userName'];
$first_name = $_REQUEST['userFirstName'];
$last_name = $_REQUEST['userLastName'];
$password = $_REQUEST['userPwd'];
$password1 = $_REQUEST['userPwd1'];
$password2 = $_REQUEST['userPwd2'];
$email= $_REQUEST['eMail'];
$website= $_REQUEST['website'];
$isNew= $_REQUEST['IsNew'];
$userID= $_REQUEST['userID'];

/*if (!isset($name)) 
{
echo "<h1> Error in name </h1>";
echo "<br />";
} else */
{
// connect to mysql
	echo "<h1> connecting to db</h1>";
		$mysql = mysqli_connect("$dbservername", "$dbusername", "$dbpassword");
	if(!$mysql) {
	echo "Cannot connect to database.";
	exit;
	}
	
	if ($password2!=$password1) 
	{
		echo "<h1> 2 password different. </h1>";
		echo "<br />";
		exit;
	}
echo "<h1> connecting to db</h1>";
echo "<h1> connected db</h1>";
// select the appropriate database
	$selected = mysqli_select_db($mysql, "$dbname");
if(!$selected) {
echo "Cannot select database.";
exit;
}
$query = "select count(*) from user  where user_name = '". $name."'" ;
$result = mysqli_query($mysql, $query);
var_dump($result);
$row = mysqli_fetch_row($result);

$count = $row[0];

	if ($count == 0) {
echo "New User";
		$query = "insert into user(user_name,user_type_id,role_id,first_name,last_name,email,website,group_id,password) values('".$name."',1,1,'".$first_name."','".$last_name."','".$email."','".$website."',1,'".$password1."') " ;		
}
else
	{
		
		$query = "UPDATE user SET first_name  = '" .$first_name. "', last_name  = '" .$last_name. "',  email  = '" .$email ."',  website = '" .$website. "', password='".$password1."' WHERE user_name = '" .$name. "' " ;
	}
$result = mysqli_query($mysql, $query);
 

echo "$query\n";

if ( $result );
{
header("Location: /OSPMS/404.php");
}

header("Location: /OSPMS/User.php");

}
?>


